<?php

namespace app\common;

use app\model\Users;
use app\model\UsersAgree;
use app\model\UsersCollect;
use app\model\UsersFollow;
use app\model\UsersFootmark;
use app\model\UsersLike;
use app\model\UsersSign;
use app\model\UsersScoreLog;
use app\model\UsersWant;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;
use think\response\Json;

class UsersService
{
	/**
	 * 执行签到+积分
	 */
	public static function score($users_id, $score_id, $score, $createtime): bool
	{
		$UsersScoreModel = new UsersSign();
		$insert = [
			'users_id' => $users_id,
			'score_id' => $score_id,
			'score' => $score,
			'createtime' => $createtime,
		];
		$UsersScoreModel->save($insert);
		self::changeScore($score_id, $users_id, '签到');
		return true;
	}
	
	/**
	 * 点赞/取消点赞
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @return bool|Json
	 */
	static function like($id, $uid, $type)
	{
		if (empty($id)) {
			writeJson(0, '参数错误');
		}
		$UsersLikeModel = new UsersLike();
		$row = $UsersLikeModel->where(['users_id' => $uid, 'type' => $type, 'pro_id' => $id])->find();
		if ($row) {
			$row->delete();
			return false;
		} else {
			$UsersLikeModel->users_id = $uid;
			$UsersLikeModel->type = $type;
			$UsersLikeModel->pro_id = $id;
			$UsersLikeModel->save();
			return true;
		}
	}
	
	/**
	 * 想要
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @return bool|Json
	 */
	static function want($id, $uid, $type)
	{
		if (empty($id)) {
			writeJson(0, '参数错误');
		}
		$UsersWantModel = new UsersWant();
		$row = $UsersWantModel->where(['users_id' => $uid, 'type' => $type, 'pro_id' => $id])->find();
		if (!$row) {
			$UsersWantModel->users_id = $uid;
			$UsersWantModel->type = $type;
			$UsersWantModel->pro_id = $id;
			$UsersWantModel->save();
			return true;
		} else {
			return false;
		}
		
	}
	
	/**
	 * 赞同表
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @param $to_uid
	 * @return bool
	 */
	static function agree($id, $uid, $type, $to_uid)
	{
		if (empty($id)) {
			writeJson(0, '参数错误');
		}
		$UsersAgreeModel = new UsersAgree();
		$row = $UsersAgreeModel->where(['users_id' => $uid, 'type' => $type, 'to_users_id' => $to_uid])->find();
		
		$UsersAgreeModel->users_id = $uid;
		$UsersAgreeModel->type = $type;
		$UsersAgreeModel->pro_id = $id;
		$UsersAgreeModel->to_users_id = $to_uid;
		$UsersAgreeModel->save();
		if ($row) {
			return false;
		} else {
			return true;
		}
		
		
	}
	
	/**
	 * 关注/取消关注
	 * @param $uid
	 * @param $to_uid
	 * @return bool
	 */
	static function follow($to_uid, $uid)
	{
		$UsersFansModel = new UsersFollow();
		$row = $UsersFansModel->where(['users_id' => $uid, 'to_users_id' => $to_uid])->find();
		if ($row) {
			$row->delete();
			return false;
		} else {
			$UsersFansModel->users_id = $uid;
			$UsersFansModel->to_users_id = $to_uid;
			$UsersFansModel->save();
			return true;
		}
		
	}
	
	/**
	 * 记录足迹
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @return bool
	 */
	static function footmark($id, $uid, $type)
	{
		if (empty($id)) {
			writeJson(0, '参数错误');
		}
		if (empty($uid)) {
			return false;
		}
		$UsersFootmakeModel = new UsersFootmark();
		$row = $UsersFootmakeModel->where(['users_id' => $uid, 'type' => $type, 'pro_id' => $id])->find();
		if ($row) {
			$row->updatetime = time();
			$row->save();
			return false;
		} else {
			$UsersFootmakeModel->users_id = $uid;
			$UsersFootmakeModel->type = $type;
			$UsersFootmakeModel->pro_id = $id;
			$UsersFootmakeModel->save();
			return true;
		}
	}
	
	/**
	 * 收藏
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @return bool
	 */
	static function collect($id, $uid, $type)
	{
		if (empty($id)) {
			writeJson(0, '参数错误');
		}
		$UsersCollectModel = new UsersCollect();
		$row = $UsersCollectModel->where(['users_id' => $uid, 'type' => $type, 'pro_id' => $id])->find();
		if ($row) {
			$row->delete();
			return false;
			
		} else {
			$UsersCollectModel->users_id = $uid;
			$UsersCollectModel->type = $type;
			$UsersCollectModel->pro_id = $id;
			$UsersCollectModel->save();
			return true;
		}
	}
	
	/**
	 * 变更积分
	 * @param $score
	 * @param $uid
	 * @param $mark
	 * @return true
	 * @throws Exception
	 */
	public static function changeScore($score, $uid, $mark)
	{
		$row = Users::lock(true)->find($uid);
		if ($score == 0 || !$row) {
			throw new Exception('变更积分失败');
		}
		
		if ($score < 0 && $row->score < abs($score)) {
			throw new Exception('积分不足');
		}
		$before = $row->score;
		$row->score += $score;
		$data = [
			'users_id' => $uid,
			'score' => $score,
			'before' => $before,
			'after' => $row->score,
			'mark' => $mark
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UsersScoreLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('积分变更失败');
		}
		
		return true;
	}
	
	
}